<!--
 * @Descripttion: 单图上传
 * @Author: zhoujj
 * @Date: 2021-07-05 16:09:18
 * @LastEditors: zhoujj
 * @LastEditTime: 2021-07-07 09:39:42
-->
<template>
  <div v-if="item">
    <el-form ref="form" :rules="rules" :model="form" label-width="120px">
      <el-form-item :label="item.title" prop="value">
        <el-upload
          class="avatar-uploader"
          :action="uploadUrl"
          :headers="uploadHeader"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
        >
          <img v-if="form.value" :src="form.value" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  props: ["item", "number", "uploadHeader", "uploadUrl"],
  data() {
    return {
      form: {
        value: "",
      },
      rules: {
        value: [
          {
            required: this.item.must,
            message: this.item.msg,
            trigger: "change",
          },
        ],
      },
    };
  },
  methods: {
    handleAvatarSuccess(res, file) {
      this.form.value = URL.createObjectURL(file.raw);
      this.$refs["form"].clearValidate();
      this.$emit("changeComponent", {
        number: this.number,
        type: this.item.type,
        value: this.form.value,
      });
    },
  },
  mounted() {
    this.form = {
      ...this.form,
      ...this.item,
    };
  },
};
</script>

<style></style>
